package com.kkwrite.regimen.consumer.controller.share;

import com.kkwrite.regimen.common.entity.dto.InDTO;
import com.kkwrite.regimen.common.entity.dto.OutDTO;
import com.kkwrite.regimen.common.entity.dto.share.ShareLogInDTO;
import com.kkwrite.regimen.consumer.controller.constant.Constant;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

import static com.kkwrite.regimen.common.constant.Constant.BOOLEAN_STRING_FALSE;

/**
 * Created by lisha on 2018/7/19 21:03.
 *
 * @author lisha
 */
@RestController
@Api(value = "分享模块")
public class ShareController {

    @Resource(name = "stringRedisTemplate")
    private StringRedisTemplate redisTemplate;
    @Resource(name = "restTemplate")
    private RestTemplate restTemplate;

    @PostMapping(value = "/share/savelog")
    @HystrixCommand(fallbackMethod = "saveShareLogError")
    @ApiOperation(value = "保存分享记录")
    @ApiImplicitParam(name = "inDTO", value = "type不能为空，当分享类型为项目详情（2）时，itemId必传，当类型不为2时，itemId必须为空",
            required = true, paramType = "body", dataType = "ShareLogInDTO")
    public OutDTO saveShareLog(@RequestBody ShareLogInDTO inDTO, @RequestHeader @ApiIgnore String token) {
        // 入参校验
        String userIdString = (String) redisTemplate.opsForHash().get(token, "userId");
        if (userIdString == null) {
            return OutDTO.of(BOOLEAN_STRING_FALSE, "002", "token invalid");
        }
        Integer type = inDTO.getType();
        if (type == null) {
            throw new RuntimeException("入参不合法");
        }
        Integer itemId = inDTO.getItemId();
        if (type == 2 && itemId == null) {
            throw new RuntimeException("入参不合法");
        }
        if (type != 2 && itemId != null) {
            throw new RuntimeException("入参不合法");
        }
        InDTO<Map<String, String>> paramDTO = new InDTO<>();
        Map<String, String> param = new HashMap<>(6);
        param.put("userId", userIdString);
        param.put("type", type.toString());
        param.put("itemId", itemId == null ? "-999" : itemId.toString());
        paramDTO.setBody(param);
        return restTemplate.postForObject(Constant.SERVICE_PREFIX + "/share/saveLog", paramDTO, OutDTO.class);
    }

    public OutDTO saveShareLogError(ShareLogInDTO inDTO, String token, Throwable t) {
        return OutDTO.of(BOOLEAN_STRING_FALSE, "保存分享日志异常！异常信息：" + t.getMessage(), null, inDTO);
    }

}
